/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.own.service;

import java.util.Arrays;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ejb.Stateless;

/**
 *
 * @author Maxim A. Kanev <ask.me.makes@gmail.com> [mkanev]
 */
@WebService()
@Stateless()
public class MyWebService {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "range")
    public String range(@WebParam(name = "input") String input) {
        if (input == null || input.length() == 0) {
            return "";
        }
        int[] inputArray = Statistics.arrayFromString(input);
        Arrays.sort(inputArray);
        return Statistics.stringFromArray(inputArray);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getMean")
    public String getMean(@WebParam(name = "input") String input) {
        if (input == null || input.length() == 0) {
            return "0";
        }
        double result = 0;
        int[] inputArray = Statistics.arrayFromString(input);
        for (int current : inputArray) {
            result += current;
        }
        return Statistics.formatOutput(String.valueOf(result / inputArray.length));
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getDeviation")
    public String getDeviation(@WebParam(name = "input") String input) {
        if (input == null || input.length() == 0) {
            return "0.0";
        }
        double result = 0.0;
        int[] inputArray = Statistics.arrayFromString(input);
        double mean = Double.parseDouble(getMean(input));
        for (int current : inputArray) {
            result += Math.pow(current - mean, 2);
        }
        result /= inputArray.length - 1;
        return Statistics.formatOutput(String.valueOf(Math.sqrt(result)));
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getInterval")
    public String getInterval(@WebParam(name = "precision") String precision,
            @WebParam(name = "input") String input) {
        if (input == null || precision==null || input.length() == 0 || precision.length() == 0) {
            return "[ NaN ; NaN ]";
        }
        int[] inputArray = Statistics.arrayFromString(input);
        double t = Statistics.getT(Double.parseDouble(precision), inputArray.length - 1);
        double mean = Double.parseDouble(getMean(input));
        double deviation = Double.parseDouble(getDeviation(input));
        return "[ "
                + Statistics.formatOutput(String.valueOf(mean - t * deviation / Math.sqrt(inputArray.length)))
                + " ; "
                + Statistics.formatOutput(String.valueOf(mean + t * deviation / Math.sqrt(inputArray.length)))
                + " ]";
    }
}
